home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / Other Langs / Tickle-4.0 (tcl) / tcl / extend / src.unused / tclXcnvclock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-26  |  4.2 KB  |  156 lines  |  [TEXT/MPS ]

  1. /* 
  2.  * tclXcnvclock.c --
  3.  *
  4.  *      Contains the TCL convertclock command.  This is in a module seperate
  5.  * from clock so that it can be excluded, along with the yacc generated code,
  6.  * since its rather large.
  7.  *-----------------------------------------------------------------------------
  8.  * Copyright 1991-1993 Karl Lehenbauer and Mark Diekhans.
  9.  *
  10.  * Permission to use, copy, modify, and distribute this software and its
  11.  * documentation for any purpose and without fee is hereby granted, provided
  12.  * that the above copyright notice appear in all copies.  Karl Lehenbauer and
  13.  * Mark Diekhans make no representations about the suitability of this
  14.  * software for any purpose.  It is provided "as is" without express or
  15.  * implied warranty.
  16.  *-----------------------------------------------------------------------------
  17.  * $Id: tclXcnvclock.c,v 2.10 1993/08/19 16:02:09 markd Exp $
  18.  *-----------------------------------------------------------------------------
  19.  */
  20.  
  21. #include "tclExtdInt.h"
  22.  
  23. static int
  24. GetTimeZone _ANSI_ARGS_((time_t  currentTime));
  25.  
  26.  
  27. /*
  28.  *-----------------------------------------------------------------------------
  29.  *
  30.  * GetTimeZone --
  31.  *   Determines the current timezone.  The method varies wildly between
  32.  * different Unix implementations, so its hidden in this function.
  33.  *
  34.  * Parameters:
  35.  *   o currentTime (I) - The clock value that is to be used for the current
  36.  *     time.
  37.  * 
  38.  * Returns:
  39.  *    Minutes east of GMT.
  40.  *-----------------------------------------------------------------------------
  41.  */
  42. static int
  43. GetTimeZone (currentTime)
  44.     time_t  currentTime;
  45. {
  46. #ifdef TCL_USE_TM_TZADJ
  47.     struct tm  *timeDataPtr = localtime (¤tTime);
  48.     int         timeZone;
  49.  
  50.     timeZone = timeDataPtr->tm_tzadj  / 60;
  51.     if (timeDataPtr->tm_isdst)
  52.         timeZone += 60;
  53.  
  54.     return timeZone;
  55. #endif
  56.  
  57. #ifdef TCL_USE_TM_GMTOFF
  58.     struct tm *timeDataPtr = localtime (¤tTime);
  59.     int        timeZone;
  60.  
  61.     timeZone = -(timeDataPtr->tm_gmtoff / 60);
  62.     if (timeDataPtr->tm_isdst)
  63.         timeZone += 60;
  64.  
  65.     return timeZone;
  66. #endif
  67.  
  68. #ifdef TCL_USE_TIMEZONE_VAR
  69.     static int setTZ = FALSE;
  70.     int        timeZone;
  71.  
  72.     if (!setTZ) {
  73.         tzset ();
  74.         setTZ = TRUE;
  75.     }
  76.     timeZone = timezone / 60;
  77.  
  78.     return timeZone;
  79. #endif
  80.  
  81. #ifdef TCL_USE_GETTIMEOFDAY
  82.     struct timeval  tv;
  83.     struct timezone tz;
  84.     int             timeZone;
  85.  
  86.     gettimeofday (&tv, &tz);
  87.     timeZone = tz.tz_minuteswest;
  88.  
  89.     return timeZone;
  90. #endif
  91.  
  92. #ifndef TCL_GOT_TIMEZONE
  93.    /*
  94.     * Cause compile error.  The defines are done in tclExtdInt.h based on
  95.     * what autoconf found.
  96.     */
  97.   error: autoconf did not figure out how to determine the timezone. 
  98. #endif
  99.  
  100. }
  101.  
  102. /*
  103.  *-----------------------------------------------------------------------------
  104.  *
  105.  * Tcl_ConvertclockCmd --
  106.  *     Implements the TCL convertclock command:
  107.  *         convertclock dateString ?GMT|{}?
  108.  *
  109.  * Results:
  110.  *     Standard TCL results.
  111.  *
  112.  *-----------------------------------------------------------------------------
  113.  */
  114. int
  115. Tcl_ConvertclockCmd (clientData, interp, argc, argv)
  116.     ClientData  clientData;
  117.     Tcl_Interp *interp;
  118.     int         argc;
  119.     char      **argv;
  120. {
  121.     long        clockVal;
  122.     time_t      baseClock;
  123.     int         zone;
  124.  
  125.     if ((argc < 2) || (argc > 4)) {
  126.         Tcl_AppendResult (interp, tclXWrongArgs, argv [0], 
  127.                           " dateString ?GMT|{}? ?baseclock?", (char *) NULL);
  128.     return TCL_ERROR;
  129.     }
  130.     if (argc == 4) {
  131.         if (Tcl_GetLong (interp, argv [3], &baseClock) != TCL_OK)
  132.             return TCL_ERROR;
  133.     } else
  134.         time (&baseClock);
  135.  
  136.     if ((argc > 2) && (argv [2][0] != '\0')) {
  137.         if (!STREQU (argv [2], "GMT")) {
  138.             Tcl_AppendResult (interp, "invalid argument: expected `GMT', ",
  139.                               "got : `", argv [2], "'", (char *) NULL);
  140.             return TCL_ERROR;
  141.         }
  142.         zone = -1; /* Force GMT */
  143.     } else {
  144.         zone = GetTimeZone (baseClock);
  145.     }
  146.  
  147.     if (Tcl_GetDate (argv [1], baseClock, zone, &clockVal) < 0) {
  148.         Tcl_AppendResult (interp, "Unable to convert date-time string \"",
  149.                           argv [1], "\"", (char *) NULL);
  150.     return TCL_ERROR;
  151.     }
  152.     sprintf (interp->result, "%ld", clockVal);
  153.     return TCL_OK;
  154. }
  155.  
  156.